package GUIHelper;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ControlLayer.RoomBookingCtr;
import ControlLayer.RoomBookingLine_RoomCtr;
import ControlLayer.RoomTypeCtr;
import GUILayer.SaleGUI;
import ModelLayer.RoomBooking;
import ModelLayer.RoomBookingLine;
import ModelLayer.RoomType;

public class GuestInPutFrame {
	private SaleGUI saleGUI;
	private JFrame frame;
	private int rtId;
	private RoomBookingCtr rbCtr;
	private RoomTypeCtr rtCtr;
	private RoomBooking rb;
	private RoomBookingLine_RoomCtr rbl_rCtr;
	private ArrayList<Integer> guestIds;
	private ArrayList<JTextField> txts;

	public GuestInPutFrame(JPanel panel, int rtId, RoomBooking rb) {
		frame = new JFrame("Check In");
		frame.setSize(350, 400);

		frame.setLocationRelativeTo(null);
		this.saleGUI = (SaleGUI) panel;
		this.rtId = rtId;
		this.rb = rb;
		initialize();
	}

	public void initialize() {
		rbl_rCtr = new RoomBookingLine_RoomCtr();
		rtCtr = new RoomTypeCtr();
		rbCtr = new RoomBookingCtr();
		RoomType rt = rtCtr.getRoomType(rtId);

		JPanel guestPanel = new JPanel();
		frame.getContentPane().add(guestPanel);
		guestPanel.setLayout(new BoxLayout(guestPanel, BoxLayout.Y_AXIS));
		JLabel space1 = new JLabel(" ");
		guestPanel.add(space1);
		JLabel lblmsn = new JLabel("A " + rt.getName()
				+ " is selected. Please input guest Id below.");
		lblmsn.setFocusable(true);
		guestPanel.add(lblmsn);
		JLabel space2 = new JLabel(" ");
		guestPanel.add(space2);

		txts = new ArrayList<JTextField>();

		for (int i = 0; i < rt.getSize(); i++) {
			JTextField txt = new JTextField();
			JLabel lbl = new JLabel("GuestId: ");
			JPanel panel = new JPanel();
			panel.setLayout(new FlowLayout());
			txt.setColumns(10);
			panel.add(lbl);
			panel.add(txt);
			guestPanel.add(panel);
			txts.add(txt);
		}

		JPanel choicePanel = new JPanel();
		JButton btnOK = new JButton("OK");
		JButton btnCancel = new JButton("Cancel");
		choicePanel.setLayout(new FlowLayout());
		choicePanel.add(btnOK);
		choicePanel.add(btnCancel);

		guestPanel.add(choicePanel);
		// TODO
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RoomBookingLine rbl = null;
				for (RoomBookingLine line : rb.getLines()) {
					if (line.getRoomType().getId() == rtId)
						rbl = line;
				}
				guestIds = new ArrayList<Integer>();
				for (int i = 0; i < txts.size(); i++) {
					if (txts.get(i) != null)
						guestIds.add(Integer.parseInt(txts.get(i).getText()));
				}
				int rId;
				rId = rbCtr.checkIn(guestIds, rtId, rb.getEndDate());
				rbl_rCtr.insertRBL_Room(rbl.getId(), rId);
				saleGUI.showRBTable(rb.getId());
				frame.dispose();
			}
		});

		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
			}
		});

	}

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

}
